<template>
	<view id="app">
		<view class="container df fdc">
			<view class="bgw df ptb40 plr15">
				<view class="w300 h180 br15 courseImg"><image :src="course.thumb"></image></view>
				<view class="f1 ml15 df fdc">
					<view class="f28 fw4 h120 t2hd">{{course.title}}</view>
					<view class="df aic cdebug f32 fw8" v-if="is_team == 1">¥{{course.team_price}}</view>
					<view class="df aic cdebug f32 fw8" v-else>¥{{course.price}}</view>
				</view>
			</view>
			<view class="bgw mt20 ver ais plr20 ptb20">
				<view class="f28 c333 pb20 bdb1s brccd">学员信息</view>
				<view class=" rows plr20 ptb30 bdb1s brcce aic">
					<view class="w150 f28 c666">学员姓名</view>
					<view class="f1">
						<uv-input placeholder="请输入姓名" border="none" v-model="name"></uv-input>
					</view>
				</view>
				<view class=" rows plr20 ptb30 aic">
					<view class="w150 f28 c666">学员手机</view>
					<view class="f1">
						<uv-input placeholder="请输入手机号" border="none" v-model="mobile"></uv-input>
					</view>
				</view>
			</view>
			<view class="bgw mt20 ver ais plr20">
				<view class=" rows plr20 ptb30 bdb1s brcce aic">
					<view class="f28 c333 ">课程价格</view>
					<view class="cdebug f32 fw8" v-if="is_team == 1">¥{{course.team_price}}</view>
					<view class="cdebug f32 fw8" v-else>¥{{course.price}}</view>
				</view>
				<view class=" rows pl20 ptb30 aic">
					<view class="f1 f28 c333 rows aic">
						<view class="c333">优惠券</view>
						<view class="c666" @click="opencoupon()" v-if=" isCoupon > 0 ">{{coupon_title}}</view>
						<view class="c666" v-else>{{coupon_title}}</view>
					</view>
					<view class="w30">
						<uv-icon name="arrow-right" size="30rpx"></uv-icon>
					</view>
				</view>
			</view>
			<view class="footer ft">
				<view class="rows bgw">
					<view class="df ais mr30 f1">
						<text class="plr30 f28">实付款</text>
						<text class="f32 fw8 cdebug">¥{{price}}</text>
					</view>
					<view class="rows">
						<view class="plr30 ptb20 bd1s bginfo brcinfo cw" @click="sumitOrder()">确认购买</view>
					</view>
				</view>
				<uv-safe-bottom></uv-safe-bottom>
			</view>
			<uv-popup ref="coupon" mode="bottom" :close-on-click-overlay="false" :closeable="true" custom-style="min-height: 400rpx;" round="15rpx">
				<view class="bgw df fdc  plr15 w100p">
					<view class="ptb30 f28 c000 fw8 df aic jcc">优惠券</view>
					<view class="w100p h340 pr oh" v-if=" isCoupon > 0">
						<scroll-view class="h340 oh" scroll-y>
							<view class="df fdc ">
								<view class="df mt20 w100p h120 shadow" v-for="(item,index) in coupon" :key="index" @click="chooseCoupon(item)">
									<view class="w250 df f32 fw8 mr20 h120 mask bgdebug aic jcc cw">¥{{item.price}}</view>
									<view class="f1 df fdc f26 fw4 h120 bgw aic jcc">
										<view class="c333" v-if="item.type == 1">通用券</view>
										<view class="c666" v-else>{{item.course}}课程专用</view>
									</view>
								</view>
								<view class="h50"></view>
							</view>
						</scroll-view>
					</view>
					<view class="h50"></view>
				</view>
			</uv-popup>
		</view>
	</view>
</template>
<script>
	let that;
	export default{
		data(){
			return {
				name:"",
				mobile:"",
				course:{},
				course_id:0,
				course_timer:{},
				course_timer_id:0,
				coupon:[],
				coupon_price:0,
				coupon_id:0,
				coupon_title:"未使用优惠券",
				is_team:0,
				isCoupon:0,
				price:0,
				order_id:0,
			}
		},
		onLoad(e){
			that =this;
			if(e.id){
				this.order_id = e.id;
			}
			
			let cart = JSON.parse(uni.getStorageSync('QLXG_cart'));
			console.log(cart);
			console.log(cart.course.id);
			this.course = cart.course;
			this.course_timer = cart.course_timer;
			this.is_team = cart.is_team;
			this.course_id = cart.course.id;
			this.course_timer_id = cart.course_timer.id;
			
			this.getUser();
			this.getCoupon();
			this.jisuan();
		},
		onShow(){
			
		},
		methods:{
			jisuan:function(){
				let price = 0;
				if(this.is_team == 1){
					price = this.course.team_price;
				}else{
					price = this.course.price;
				}
				this.price = price - this.coupon_price;
			},
			opencoupon:function(){
				this.$refs.coupon.open();
			},
			chooseCoupon:function(e){
				this.coupon_price = e.price;
				this.coupon_id = e.id;
				this.coupon_title = "-"+e.price;
				this.jisuan();
				this.$refs.coupon.close();
			},
			getUser:function(){
				this.request({
					url:"/api/cart/user",
					success:function(res){
						that.mobile = res.data.mobile
						that.name = res.data.name
					}
				})
			},
			getCoupon:function(){
				this.request({
					url:"/api/cart/coupon",
					data:{course_id:this.course.id},
					success:function(res){
						if(res.code == 200){
							that.isCoupon = res.data.length;
							that.coupon = res.data;
						}
					}
				})
			},
			//提交订单
			sumitOrder:function(){
				this.request({
					url:"/api/cart/order",
					data:{
						'course_id':this.course.id,
						'is_team': this.is_team,
						'course_time_id':this.course_timer.id,
						'coupon_id':this.coupon_id,
						'order_id':this.order_id,
					},
					success:function(res){
						if(res.code == 200){
							uni.requestPayment({
								provider: 'wxpay',
								timeStamp: res.data.timeStamp,
								nonceStr: res.data.nonceStr,
								package: res.data.package,
								signType: res.data.signType,
								paySign: res.data.paySign,
								success:function(rlt){
									console.log(rlt);
									if (rlt.errMsg == "requestPayment:ok") { // 调用支付成功
										that.request({
											url:'/api/cart/pay',
											data:{id:res.order_id},
											success:function(rel){
												if(rel.code ='200'){
													if(rel.data.order_type == '2'){ //待分享订单
														uni.redirectTo({
															url:"/pages/member/order/share?id="+rel.id
														})
													}else{
														uni.redirectTo({
															url:"/pages/member/order/detail?id="+rel.id
														})
													}
												}
											}
										})
									} else if (rlt.errMsg == 'requestPayment:fail cancel') {// 用户取消支付的操作
										  that.alert('支付取消');
									}
								},
								fail:function(rlt){
									if (rlt.errMsg == 'requestPayment:fail cancel') {// 用户取消支付的操作
										  that.alert('支付取消');
									}
								}
							})
						}
					}
				})
			}
		}
	}
</script>

<style scoped>
	
</style>